#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPlainTextEdit>
#include <QProcess>
#include <QFileDialog>
#include <QMessageBox>
#include <QSplitter>
#include <QTextEdit>
#include <QToolBar>
#include <QStatusBar>
#include <QLabel>
#include <QComboBox>

class CodeEditor;
class QLineEdit;

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void newFile();
    void openFile();
    bool saveFile();
    bool saveAsFile();
    void compileCode();
    void runCode();
    void compileAndRun();
    void clearOutput();
    void runPython();

    void updateStatusBar();
    void processOutput();
    void processError();
    void processFinished(int exitCode, QProcess::ExitStatus exitStatus);

    void about();
    void onInputEntered();

    void zoomIn();
    void zoomOut();
    void resetZoom();

    // 调试相关
    void debugCode();
    void stepInto();
    void stepOver();
    void stepOut();
    void continueExecution();
    void pauseExecution();
    void addBreakpoint(int line);
    void removeBreakpoint(int line);
    void toggleBreakpoint(int line);
    void updateBreakpointsDisplay();
    void debugFinished(int exitCode, QProcess::ExitStatus exitStatus);
    void clearBreakpoints();

private:
    void setupUI();
    void setupMenuBar();
    void setupToolBar();
    void setupStatusBar();
    void setupConnections();

    bool maybeSave();
    void loadFile(const QString &fileName);
    bool saveFile(const QString &fileName);

    void setBreakpoints(const QList<int> &bp);

    CodeEditor *codeEditor;
    QTextEdit *outputConsole;
    QProcess *compileProcess;
    QProcess *runProcess;
    QProcess *debugProcess;
    QLineEdit *inputLineEdit;

    QString currentFile;
    QLabel *statusLabel;
    QComboBox *compilerComboBox;

    bool isDebugging;
    QList<QPair<int, int>> breakpoints;

    // 调试菜单项
    QAction *debugAction = nullptr;
    QAction *stepIntoAction = nullptr;
    QAction *stepOverAction = nullptr;
    QAction *stepOutAction = nullptr;
    QAction *continueAction = nullptr;
    QAction *pauseAction = nullptr;
    QAction *clearBreakpointsAction = nullptr;
    QAction *compileAction = nullptr;
    QAction *runAction = nullptr;
    QAction *compileRunAction = nullptr;
};

#endif // MAINWINDOW_H
